import {defineStore} from 'pinia'
import { ref } from 'vue'
import { reqLogin, reqUserInfo } from '../../api/login';



export const useUserStore=defineStore('user',()=>{
  let token=ref<string>();
  if( localStorage.getItem('himalay_token')){
    token.value=localStorage.getItem('himalay_token')!
  };

  const userInfo=ref<any>();

  //登录的功能函数
  const getLogin = async(phone:string,password:string) => {
    try {
      let res = await reqLogin(phone,password);
      if(res.code == 200){
        token.value=res.data.token;
        localStorage.setItem('himalay_token',res.data.token);
      }
    } catch (error) {
      console.log(error)
    }
   };
  
   const getUserInfo=async()=>{
    try {
      let res=await reqUserInfo();
      if(res.code==200){
        userInfo.value=res.data
      }
    } catch (error) {
      console.log(error);
      
    }
   };
  const clearUserInfo=()=>{
    localStorage.removeItem('himalay_token');
    userInfo.value=null;
  }


   return {
    token,
    getLogin,
    getUserInfo,
    userInfo,
    clearUserInfo
   }


})